package com.woniuxy.verify.controller;

import com.woniuxy.verify.bean.ResultMsg;
import com.woniuxy.verify.bean.StudentBean;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author linke
 * @version 1.0
 * @className: RestFulController
 * @description: RestFul方式传值 控制器
 * @date 2021/11/14 16:58
 *
 * 通过注解的方式 区分请求的类型
 */
@RequestMapping("students")
@RestController
public class RestFulController {
    /**
     * 新增
     * @param studentBean
     * @return
     */
    @PostMapping(value = "/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg saveStudentBean(StudentBean studentBean){
        ResultMsg resultMsg = new ResultMsg("200","操作成功");

        System.out.println(studentBean);

        return resultMsg;
    }

    /**
     * 前端：http://localhost:8080/mvc/students/1   students/2    1 2 代表的是id
     *
     * 修改
     * @param studentBean
     * @param id
     * @return
     * @PathVariable 获取对应的属性值
     */
    @PutMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateStudentBean(StudentBean studentBean,@PathVariable("id") Long id){
        ResultMsg resultMsg = new ResultMsg("200","操作成功");

        System.out.println(studentBean);

        return resultMsg;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg deleteStudentBean(@PathVariable("id") Long id){
        ResultMsg resultMsg = new ResultMsg("200","操作成功");

        System.out.println(id);

        return resultMsg;
    }

    /**
     * @PathVariable 是spring3.0 的一个新功能：接收请求路径中占位符的值
     * 查询
     * @param id  可在路径上显示的参数
     * @return
     */
    @GetMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg getStudentBeanById(@PathVariable("id") Long id){
        ResultMsg resultMsg = new ResultMsg("200","操作成功");

        System.out.println(id);

        StudentBean studentBean = new StudentBean();
        studentBean.setStuName("小二");


        resultMsg.setData(studentBean);
        return resultMsg;
    }

    /**
     *  传值方式匹配
     *  uri === students/all/张三/18/4578
     * @param stuName
     * @return
     */
    @GetMapping(value="/all/{stuName}/{age}/{stuNo}",produces = {"application/json;charset=utf-8"})
    public ResultMsg findAllStudents(@PathVariable("stuName") String stuName,
                                     @PathVariable("age")Integer age,
                                     @PathVariable("stuNo") String stuNo){

        ResultMsg resultMsg = new ResultMsg("200","操作成功");

        System.out.println(stuName);
        System.out.println(age);
        System.out.println(stuNo);

        StudentBean studentBean = new StudentBean();
        studentBean.setStuName("小二");
        StudentBean studentBean02 = new StudentBean();
        studentBean02.setStuName("小二");


        List<StudentBean> datas = new ArrayList<>();
        Collections.addAll(datas,studentBean,studentBean02);

        //将集合转入到 数据返回对象  并返回页面
        resultMsg.setData(datas);
        return resultMsg;
    }
}
